package com.google.code.struts2jruby.ognl;

import junit.framework.TestCase;
import ognl.Ognl;
import ognl.OgnlException;
import ognl.OgnlRuntime;
import org.jruby.Ruby;
import org.jruby.runtime.builtin.IRubyObject;

/**
 * @author Richard L. Burton III
 */
public class RubyObjectPropertyAccessorTestCase extends TestCase {

    Script script = new Script("./src/test/resources/example/");

    Ruby ruby;

    public RubyObjectPropertyAccessorTestCase() {
        OgnlRuntime.setPropertyAccessor(IRubyObject.class, new RubyObjectPropertyAccessor());
    }

    protected void setUp() throws Exception {
        ruby = Ruby.getDefaultInstance();

    }

    protected void tearDown() throws Exception {
        ruby.tearDown();
    }

    public void testGetProperty() throws OgnlException {
        script.setName("BasicAction.rb");
        ruby.loadFile(script.getFile());
        IRubyObject object = ruby.evalScript(script.getNewSyntax());
        assertEquals("Richard", Ognl.getValue("name", object).toString());
    }

    public void testSetProperty() throws OgnlException {
        script.setName("BasicAction.rb");
        ruby.loadFile(script.getFile());
        IRubyObject object = ruby.evalScript(script.getNewSyntax());
        Ognl.setValue("name", object, "Burton");
        assertEquals("Burton", Ognl.getValue("name", object).toString());
    }

//    public void testSetComplexProperty() throws OgnlException {
//        script.setName("BasicAction.rb");
//        ruby.loadFile(script.getFile());
//        IRubyObject object = ruby.evalScript(script.getNewSyntax());
//        Ognl.setValue("person.age", object, "28");
//        assertEquals("28", Ognl.getValue("person.age", object).toString());
//    }
}
